package com.example.testdemo.gradient_progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.testdemo.R;

/**
 * Created by 颜 on 04.12.012.
 * 渐变Progress
 */

public class GradientProgress extends View {
    private Paint mPaint;// 设置个新的长方形
    private Paint strokePaint;// 描边画笔
    private RectF strokeRect;//描边区3域
    private RectF oval1;
    private int progress;//进度
    private int progressPx;//像素进度
    private PorterDuffXfermode mode;//合并样式
    private boolean isGradient;//是否采用渐变色
    private int backgroundColor;//背景颜色
    private int progressColor;//进度条颜色
    private int progressPadding;//进度条padding
    private int circleAngle;//圆角度数
    private int borderColor;//边框颜色
    private int borderWidth;//边框宽度

    public GradientProgress(Context context) {
        super(context);
    }

    public GradientProgress(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initParams(context, attrs);
        initPoint();
    }

    public GradientProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    /**
     * 获取xml中的参数
     */
    private void initParams(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.GradientProgress);
        isGradient = array.getBoolean(R.styleable.GradientProgress_gradient,false);
        backgroundColor = array.getColor(R.styleable.GradientProgress_backgroundColor,Color.LTGRAY);
        progressColor = array.getColor(R.styleable.GradientProgress_progressColor,Color.RED);
        circleAngle = array.getDimensionPixelOffset(R.styleable.GradientProgress_circleAngle,0);
        borderWidth = array.getDimensionPixelOffset(R.styleable.GradientProgress_borderWidth,0);
        borderColor = array.getColor(R.styleable.GradientProgress_borderColor,Color.TRANSPARENT);
        progressPadding = array.getDimensionPixelOffset(R.styleable.GradientProgress_padding,0);
        array.recycle();
    }

    private void initPoint() {
        mPaint = new Paint();
        mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
        strokePaint = new Paint();
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(borderWidth);
        strokePaint.setColor(borderColor);
        strokePaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        oval1 = new RectF(0, 0, getWidth(), getHeight());
        int strokeLine = borderWidth / 2;
        strokeRect = new RectF(strokeLine, strokeLine, getWidth() - strokeLine, getHeight() - strokeLine);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //保存当前状态
        int saveLayer = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
        //清除并设置画笔样式
        mPaint.reset();
        mPaint.setStyle(Paint.Style.FILL);//充满
        mPaint.setColor(backgroundColor);
        mPaint.setAntiAlias(true);// 设置画笔的锯齿效果
        //画个圆角背景
        canvas.drawRoundRect(oval1, circleAngle, circleAngle, mPaint);
        //清除样式，设置前景色
        mPaint.reset();
        mPaint.setColor(progressColor);
        mPaint.setXfermode(mode);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setAntiAlias(true);
        //画进度
        canvas.drawRect(progressPadding, progressPadding, progressPx, getHeight() - progressPadding, mPaint);
        mPaint.setXfermode(null);
        //画描边
        if (borderWidth != 0) {
            canvas.drawRoundRect(strokeRect, circleAngle, circleAngle, strokePaint);
        }
        //恢复状态
        canvas.restoreToCount(saveLayer);
    }


    public void setProgress(int progress) {
        this.progress = progress;
        progressPx = (int) (getWidth() * (progress / 100f));
        postInvalidate();
    }

    public int getProgress(){
        return progress;
    }
}
